/**
 * @name zhangyy-g@grandsoft.com.cn
 * @description zhangyy's code
 * Date: 15-4-17
 * Time: 下午5:52
 *
 */
(function ($) {
    var docList = {

        __container: null,
        __options: null,

        __path: null,
        __filter: null,

        __cache: {},

        __collection: new (Backbone.Collection.extend({
            model: Backbone.Model,
            parse: function (response, xhr) {
                if (response.code != 0) {
                    return []
                }
                var items = response.data.subfiles || response.data.data.subfiles
                if (!items) return response
                _.each(items, function (item) {
                    item.path = item.path || item.filePath
                    item.dir =  item.dir || item.isDir || item.isdir
                })

                return items
            }
        })),

        init: function (container, options) {
            var self = this
            self.__container = container
            self.__options = options

            self.__collection.bind('reset', self.addItems, self)
            self.__collection.bind('add', self.addItem, self)
        },

        load: function (url, filter, data, callback) {
            var self = this
            self.__container.empty()
            self.__filter = filter
            self.__url = url
            self.__load(self.__url, self.__filter, data, callback)
        },

        __load: function (url, filter, data, callback) {
            var self = this
            self.__collection.fetch({
                url: url,
                method: 'post',
                data: data,
                success: function (collection, response, options) {
                    //self.__cache[url] = response
                    if (collection.length == 0) {
                        self.__showEmptyTip()
                    }
                    callback && callback(collection, response, options)

                    $.jps.publish('search-update')
                },
                error: function () {
                    callback && callback(null)
                }
            })
        },

        resetCollection: function (items, filter) {
            var self = this
            var collection = self.__collection
            if (filter) {
                self.__filter = filter
            }
            self.__container.empty()
            collection.reset(items)

            if (collection.length == 0) {
                self.__showEmptyTip()
            }
        },

        reload: function (callback) {
            var self = this
            self.__load(self.__url, self.__filter, callback)
        },

        addItems: function (collection, options) {
            var self = this
            collection.each(function (model) {
                self.addItem(model, collection)
            })
        },

        addItem: function (model, collection, options) {
            var self = this
            model.set('filter', self.__filter)
            var view = new App.modules.docView({
                model: model
            })
            self.__container.append(view.render().$el)
        },

        __showEmptyTip: function () {
            var self = this
            var container = self.__container
            var message = self.__filter == 'search' ? '没有搜索到相关文件' : '空文件夹'
            container.html(Mustache.render(emptyTpl, {message: message}))
        },

        getCheckedItems: function () {
            var self = this

            var items = self.__collection.where({
                isChecked: true
            })

            return items
        }
    }

    var emptyTpl = '<div class="empty">{{message}}</div>'
    App.modules.docList = docList
})(jQuery)